package com.fuze.navi.controller;

import cn.hutool.core.net.Ipv4Util;
import cn.hutool.extra.servlet.ServletUtil;
import com.fuze.navi.common.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.time.Year;

/**
 * @author sepc_
 * @date 5/25/2021 16:17
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class TestController {

    final RestTemplate restTemplate;

    /**
     * 测试IP定位
     *
     * @param request 请求对象
     * @return 定位数据
     */
    @GetMapping("testLoc")
    public Result testLoc(HttpServletRequest request) {
        String url = "https://restapi.amap.com/v5/ip";
        String key = "?key=cd3b8142ea2c67ab7a29b3a3e91f34a4";
        String type = "&type=4";
        String clientIp = ServletUtil.getClientIP(request);
        String ip = "&ip=" + clientIp;
        String addr = url + key + type + ip;
        String forObject = restTemplate.getForObject(addr, String.class);
        return Result.ok(forObject);
    }

}
